package net.zjitc.controller;

import net.zjitc.pojo.Category;
import net.zjitc.pojo.Product;
import net.zjitc.service.CategoryService;
import net.zjitc.service.ProduceService;
import net.zjitc.service.impl.CategoryServiceImpl;
import net.zjitc.service.impl.ProductServiceImpl;
import net.zjitc.utils.PageBean;
import net.zjitc.utils.dispacher.URLMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by Administrator on 2017/12/6.
 */
public class UserProduct {
    CategoryService categoryService = new CategoryServiceImpl();
    ProduceService produceService = new ProductServiceImpl();
    int pageSize = 12;
    //通过一级分类，页面显示的左边需要一级分类与二级分类名称
    @URLMapping(url = "/findByCid")
    public String findByCid(HttpServletRequest request, HttpServletResponse response){
        int page = Integer.valueOf(request.getParameter("page"));
        int cid = Integer.valueOf(request.getParameter("cid"));

        List<Category> clist = categoryService.findAll();
        request.setAttribute("clist",clist);

        //准备一级分类下的所有产品 ，按分页
        PageBean<Product> pageBean = produceService.findPageBeanByCid(page,pageSize,cid);
        request.setAttribute("page",pageBean);

        /*一级分类的cid，用于分页查询用*/
        request.setAttribute("cid",cid);
        return "/WEB-INF/user/productList.jsp";
    }

    /**
    通过二级分类查询产品，页面显示的左边需要一级分类与二级分类名称
     为什么准备两个页面？？
     因为页面有分页功能，根据页码进行查询的时候，
     分类查询参数是一级分类或者二级分类，它们是不一样的，所有用两个页面做区分，但是页面显示效果基本一样
    */
    @URLMapping(url = "/findByCsid")
    public String findByCsid(HttpServletRequest request, HttpServletResponse response){
        //左则栏中的数据
        List<Category> clist = categoryService.findAll();
        request.setAttribute("clist",clist);
        //
        return "/WEB-INF/user/product_secondList.jsp";
    }

    @URLMapping(url = "/findByPid")
    public String findByPid(HttpServletRequest request, HttpServletResponse response){
        int pid = Integer.valueOf(request.getParameter("pid"));
        //左则栏中的数据
        List<Category> clist = categoryService.findAll();
        request.setAttribute("clist",clist);
        //商品数据准备
        Product product = produceService.findByPid(pid);
        request.setAttribute("p",product);
        return "/WEB-INF/user/product.jsp";
    }

}
